#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=105;
int n,k;
ll a[N];
vector<ll> v[N];
ll ans,s;
void dfs(int no){
    if(s>ans)return;
    if(no==n+1){
        ans=min(ans,s);
        return;
    }
    for(int i=k;i;--i){
        if(v[i].size()&&v[i].back()==a[no]){
            if(i==k)return;
            if(v[i+1].size()==n/k)break;
            ++i;
            if(v[i].size())s+=a[no]-v[i].back();
            v[i].push_back(a[no]);
            dfs(no+1);
            v[i].pop_back();
            if(v[i].size())s-=a[no]-v[i].back();
            return;
        }
    }
    for(int i=1;i<=k;++i){
        if(v[i].size()==n/k)continue;
        if(v[i].size()&&v[i].back()==a[no])continue;
        if(v[i].size())s+=a[no]-v[i].back();
        v[i].push_back(a[no]);
        dfs(no+1);
        v[i].pop_back();
        if(v[i].size())s-=a[no]-v[i].back();
    }
}
int main(){
    freopen("diyiti.in","r",stdin);
    freopen("diyiti.out","w",stdout);
    cin>>n>>k;
    ans=0x3f3f3f3fll*N;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    dfs(1);
    if(ans==0x3f3f3f3fll*N)ans=-1;
    cout<<ans;
    return 0;
}
